import Vue from 'vue'	
import Vuex from 'vuex'
	
Vue.use(Vuex)
const state = {
	cartList: []
}

const store = new Vuex.Store({
	state,
	mutations:{
		addToMutation(state,payload){
			payload.checked = true
			state.cartList.push(payload)
			console.log(state.cartList)
		},
		addCounter(state,payload){
			payload.count++
		}
	},
	actions:{
		addCart(context, payload){
			return new Promise((resolve, reject) => {
				//1.查找之前数组中是否有该商品
				let oldProduct = context.state.cartList.find(item => item.iid === payload.iid)
				if(oldProduct){
					console.log(payload)
					context.commit('addCounter', oldProduct)
					resolve('商品数量+1')
				}else{
					 payload.count = 1
					context.commit('addToMutation',payload)
					resolve('添加商品成功')
				}
			
			})
		
		}
	}
})

export default store
